/**
 * Created by ZQZQQD on 2017/3/6.
 */
$(function(){
    /*------------------公共数据------------------*/
    var identity="";                                    //人员类型
    var education="";                                   // 学历
    var credit="";                                      // 信用情况
    /*-------------------学生党------------------*/
    var EntranceTime="";                                // 入学时间(注明：需要时间弹出框,值中需加'Time')
    var isTaoId="";                                      //是否有淘宝账号
    /*---------------------上班族-------------------*/
    var companyType="";                                 // 公司类型
    var workingLife=""                                  //工作年限
    var socialSecurity=""                               //是否连续6个月保险
    var creditard=""                                    //是否有信用卡
    var housingFund=""                                    //是否有6个月公积金

    /*初始化mui对象*/
    var picker = new mui.PopPicker();
    var identitys = new mui.PopPicker();
    var educations = new mui.PopPicker();
    var credits = new mui.PopPicker();
    var isTaoIds=new mui.PopPicker();
    var companyTypes=new mui.PopPicker();
    var workingLifes= new mui.PopPicker();
    var socialSecuritys= new mui.PopPicker();
    var creditards= new mui.PopPicker();
    var housingFunds= new mui.PopPicker();
    var myDate = new Date();

    var nowYear=myDate.getFullYear();


    /*----------------------------时间绑定-------------------------------*/
    var options = JSON.parse('{"type":"date","beginYear":"1949","endYear":'+nowYear+'}');
    var EntranceTimes = new mui.DtPicker(options);
    /*--------------------------初始化数据------------------------------*/
    identitys.setData([{value: "0", text: "学生党"},{value: "1", text: "上班族"}]);
    educations.setData([{value: 1, text: '大专以下'}, {value: 2, text: '大专'}, {value: 3, text: '本科'}, {value: 4, text: '研究生及其以上'}]);
    credits.setData([{value: 1, text: '信用记录良好'}, {value: 2, text: '少量逾期'}, {value: 3, text: '征信较差'}]);
    isTaoIds.setData([{value: "1", text: "是"},{value: "0", text: "否"}]);
    companyTypes.setData([{value: 1, text: '政府或企事业单位'}, {value: 2, text: '国企央企'}, {value: 3, text: '外资企业'}, {value: 4, text: '上市公司'},{value: 5, text: '普通企业'}, {value: 6, text: '其他 '}]);
    workingLifes.setData([{value: 1, text: '1年以下'}, {value: 2, text: '1-3年'}, {value: 3, text: '3-5年'}, {value: 4, text: '5年以上'}]);
    socialSecuritys.setData([{value: 1, text: '是'}, {value: 0, text: '否'}]);
    creditards.setData([{value: 1, text: '是'}, {value: 0, text: '否'}]);
    housingFunds.setData([{value: 1, text: '是'}, {value: 0, text: '否'}]);

    /*---------------------------绑定数据-------------------------------*/


    pickevent('identity', identitys, identity,"identity1");
    pickevent('education', educations, education,"education1");
    pickevent('credit', credits, credit,"credit1");
    pickevent('EntranceTime',EntranceTimes,EntranceTime,'EntranceTime1');
    pickevent('isTaoId',isTaoIds,isTaoId,'isTaoId1');

    pickevent('companyType',companyTypes,companyType,'companyType1');
    pickevent('workingLife',workingLifes,workingLife,'workingLife1');
    pickevent('socialSecurity',socialSecuritys,socialSecurity,'socialSecurity1');
    pickevent('creditard',creditards,creditard,'creditard1');
    pickevent('housingFund',housingFunds,housingFund,'housingFund1');


    /* -----------------------绑定弹出层事件---------------------------*/
    function pickevent(mm, nn, cc, dd) {
        document.getElementById(mm).addEventListener('tap', function() {
               if(mm.indexOf("Time")>0){
                   nn.show(function(e) {
                       document.getElementById(mm).innerText =e.text;
                   });
               }else{
                   nn.show(function(e) {
                       document.getElementById(mm).innerText = e[0].text;
                       cc = e[0].value;
                       document.getElementById(mm).style.color = 'black';
                       sessionStorage.setItem(dd, cc);
                   });
               }
        }, false);
    }

    /* -------------------查询个人信息返回值做操作---------------------*/

    function queryInformation(){
        var uid=localStorage.getItem('userid');
        console.log(uid);
        data={
            userid:uid
        };
        util.ajax('selectbyid',data,queryInformationHander);
    }
    function queryInformationHander(data){
        var data=data.data;
        console.log(data);
        if(data.real_name){
            $('#myname').html(data.real_name);
            $('#readName').val(data.real_name);
            $('#carID').val(data.user_idnum);
            sessionStorage.setItem('identity1',data.identity);
            sessionStorage.setItem('education1',data.edu_degree);
            sessionStorage.setItem('credit1',data.credit_status);
            switch (data.identity){
                case 0:
                    $('#occupation').html("学生党");
                    break;
                case 1:
                    $('#occupation').html("上班族");
                    break;
            }
            switch (data.edu_degree){
                case 1:
                    $('#education').html('大专以下');
                    break;
                case 2:
                    $('#education').html('大专');
                    break;
                case 3:
                    $('#education').html('本科');
                    break;
                case 4:
                    $('#education').html('研究生及其以上');
                    break;
            }
            switch (data.credit_status){
                case 1:
                    $('#credit').html('信用记录良好');
                    break;
                case 2:
                    $('#credit').html('少量逾期');
                    break;
                case 3:
                    $('#credit').html('征信较差');
                    break;
            }
        }
    }
    queryInformation();

    /* -----------------------下一步点击事件---------------------------*/
    $('#next').click(function(){
        var isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;// 15位身份证正则表达式
        var  isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;// 18位身份证正则表达式
        var readName= $("#readName").val();
        var carID= $("#carID").val();
        var identity=sessionStorage.getItem('identity1');
        var educations=sessionStorage.getItem('education1');
        var credit_status=sessionStorage.getItem('credit1');
        sessionStorage.setItem('carID',carID);
        sessionStorage.setItem('readName',readName);



        if(isIDCard1.test(carID)||isIDCard2.test(carID)){
            if(identity && educations && credit_status && readName){
                selectbytype(identity);
                if(identity==0){
                    $('#myInformationFirst').css('display','none');
                    $('#student').css('display','block');

                }else if(identity==1){
                    $('#myInformationFirst').css('display','none');
                    $('#officeWorker').css('display','block');
                }
            }else{
                console.log("请完善资料");
            }
        }else{
            console.log("请准确填写身份证");
        }
    });

    /* ----------------------获取用户类型基本信息----------------------*/
    function selectbytype(id){
       var userID= localStorage.getItem('userid');
        var data={
            'userid':userID,
            'type':id
        };
        util.ajax('selectbytype',data,selectbytypeHander);
    }
    function selectbytypeHander(data){
      var data=data.data;
        var id=sessionStorage.getItem('occupation1');
        if(id==1){
            if(data.school_name){
                $('#schollName').val(data.school_name);
                $('#EntranceTime').html(data.admission_time);
                sessionStorage.setItem('isTaoId1',data.tb_account);
                switch (data.tb_account){
                    case 1:
                        $('#isTaoId').html("是");
                        break;
                    case 2:
                        $('#isTaoId').html("否");
                        break;
                }
               $('#urgentName').val(data.urgent_name);
               $('#urgentPhone').val(data.urgent_tel);
            }
        }else{
            if(data.company_type){
                var companyType=$('#companyType');
                var workingLife=$('#workingLife');
                var creditard=$('#creditard');
                var socialSecurity=$('#socialSecurity');
                var housingFund=$('#housingFund');
                sessionStorage.setItem('companyType1',data.company_type);
                sessionStorage.setItem('workingLife1',data.working_life);
                sessionStorage.setItem('creditard1',data.credit_card);
                sessionStorage.setItem('socialSecurity1',data.social_security);
                sessionStorage.setItem('housingFund1',data.housing_fund);
                switch(data.company_type){
                    case 1:
                        companyType.html("政府或企事业单位");
                        break;
                    case 2:
                        companyType.html("国企央企");
                        break;
                    case 3:
                        companyType.html("外资企业");
                        break;
                    case 4:
                        companyType.html("上市公司");
                        break;
                    case 5:
                        companyType.html("普通企业");
                        break;
                    case 6:
                        companyType.html("其他");
                        break;
                }
                switch(data.working_life){
                    case 1:
                        workingLife.html("1年以下");
                        break;
                    case 2:
                        workingLife.html("1-3年");
                        break;
                    case 3:
                        workingLife.html("3-5年");
                        break;
                    case 4:
                        workingLife.html("5年以上");
                        break;
                }
                switch(data.credit_card){
                    case 1:
                        creditard.html("是");
                        break;
                    case 0:
                        creditard.html("否");
                        break;

                }
                switch(data.social_security){
                    case 1:
                        socialSecurity.html("是");
                        break;
                    case 0:
                        socialSecurity.html("否");
                        break;

                }
                switch(data.housing_fund){
                    case 1:
                        housingFund.html("是");
                        break;
                    case 0:
                        housingFund.html("否");
                        break;

                }
            }
        }
    }

    /* ---------------------------提交客户信息公共方法-----------------*/
    function saveuserinfo(data){
        util.ajax('saveuserinfo',data,saveuserinfoHnader);
    }
    function saveuserinfoHnader(data){
            console.log(data);
    }
    /* -------------------------学生党提交信息-------------------------*/
    $('#studentSuccess').click(function(){
        var schoolName=$('#schollName').val();
        var EntranceTime=$('#EntranceTime').html();
        var isTb=sessionStorage.getItem('isTaoId1');
        var urgentName=$('#urgentName').val();
        var urgentPhone=$('#urgentPhone').val();
        if(schoolName && EntranceTime && isTb && urgentName &&urgentPhone){
            var identity=sessionStorage.getItem('identity1');
            var educations=sessionStorage.getItem('education1');
            var credit_status=sessionStorage.getItem('credit1');
            var carID=sessionStorage.getItem('carID');
            var  readName= sessionStorage.getItem('readName');
            var userID= localStorage.getItem('userid');
            var data={
                'userid':userID,                        //用户ID - 必填
                'identity':identity,                    //用户身份
                'real_name':readName,                   //真实姓名
                'user_idnum':carID,                     //身份证
                'edu_degree':educations,                //文化程度
                'credit_status':credit_status,          //信用程度

                'school_name':schoolName,               //学校名称
                'admission_time':EntranceTime,          //入学时间
                'tb_account':isTb,                      //是否有淘宝
                'urgent_name':urgentName,               //紧急联系人姓名
                'urgent_tel':urgentPhone                //紧急联系人电话
            };
            saveuserinfo(data);
        }else{
                console.log('请完善资料');
        }
    });

    /* -------------------------学生党上一步操作-----------------------*/
    $('.student-parp').click(function(){
        $('#myInformationFirst').css('display','block');
        $('#student').css('display','none');
    });

    /* -------------------------上班族提交信息-------------------------*/
    $('#officeWorkerSuccess').click(function(){
      var companyType1= sessionStorage.getItem('companyType1');
      var workingLife1= sessionStorage.getItem('workingLife1');
      var creditard1 =sessionStorage.getItem('creditard1');
      var socialSecurity1 =sessionStorage.getItem('socialSecurity1');
      var housingFund1= sessionStorage.getItem('housingFund1');
      if(companyType1 && workingLife1 &&creditard1 && socialSecurity1 &&housingFund1){
          var identity=sessionStorage.getItem('identity1');
          var educations=sessionStorage.getItem('education1');
          var credit_status=sessionStorage.getItem('credit1');
          var carID=sessionStorage.getItem('carID');
          var  readName= sessionStorage.getItem('readName');
          var userID= localStorage.getItem('userid');

          var data={
              'userid':userID,                        //用户ID - 必填
              'identity':identity,                    //用户身份
              'real_name':readName,                   //真实姓名
              'user_idnum':carID,                     //身份证
              'edu_degree':educations,                //文化程度
              'credit_status':credit_status,          //信用程度

              'company_type':companyType1,           //公司类型
              'working_life':workingLife1,           //公司年限
              'credit_card':creditard1,             //是否有信用
              'social_security':socialSecurity1,       //是否6个月以上保险
              'housing_fund':housingFund1            //是否6个月以上房屋公积金

          }
      }

    });
    /* -------------------------上班族上一步操作-----------------------*/
    $('.office-worker-studentPxp').click(function(){
        $('#myInformationFirst').css('display','block');
        $('#officeWorker').css('display','none');
    });

    /*----------------------查询是否有未读记录--------------------------*/
});


